import { Component, Input, OnInit } from '@angular/core';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { BehaviorSubject } from 'rxjs';
import { Consts } from 'src/app/consts/consts';
import { RoleUserQueryParam } from 'src/app/models/param/role-user-query.param';
import { UserDto } from 'src/app/models/sys/user.dto';
import { ApiService } from 'src/app/services/api.service';

@Component({
  selector: 'app-role-users',
  templateUrl: './role-users.component.html',
  styleUrls: ['./role-users.component.less']
})
export class RoleUsersComponent implements OnInit {
  @Input() paramSubject = new BehaviorSubject<RoleUserQueryParam>(new RoleUserQueryParam());

  param = new RoleUserQueryParam();

  users: UserDto[] = [];
  pageIndex = 1;
  pageSize = 10;
  total = 0;

  constructor(private _api: ApiService) {

  }

  ngOnInit(): void {
    this.paramSubject.subscribe((value) => {
      this.param = value;
      this.onSearch();
    })
  }

  onSearch() {
    this.param.pageNum = this.pageIndex;
    this.param.pageSize = this.pageSize;
    this._api.getUsersByRole(this.param).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK && resp.data) {
        this.users = resp.data;
        this.total = resp.total;
      }
    });
  }

  onPageParamChange(params: NzTableQueryParams) {
    this.pageIndex = params.pageIndex;
    this.pageSize = params.pageSize;
    this.onSearch();
  }

  onUserDetail() {
    // TODO
  }
}
